<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


/**
 * Class for generating shift notification pdf.
 *
 * @category RedCross
 * @package RedCross_Pdf
 */

class RedCross_Pdf_ShiftNotification extends RedCross_Pdf_Base
{
    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct('Vaktutkalling');
    }


    /**
     * Draws the shift notification with the provided details. This is
     * not by any means pretty, but it sort of looks ok, and gets the
     * job done.
     */
    public function generate(RedCross_Gateway_Base_Shift $shift)
    { 
        $appearance_date = new RedCross_Date($shift->appearance_time, RedCross_Date::DB_FMT);
        $appearanceString = $appearance_date->getTimeStr();
        $from_date = new RedCross_Date($shift->from_date, RedCross_Date::DB_FMT);
        $to_date = new RedCross_Date($shift->to_date, RedCross_Date::DB_FMT);
        if ($appearance_date == $from_date) 
        {
            $appearanceString = 'Direkte på arrangement';
        }
        $noti = array(self::make_row(self::make_cell('UTKALLING TIL ' . $shift->name, 196, 1, 'B', 'C', 24)),
            self::make_row(self::make_cell('TID / STED', 46, 1, 'B'),
                self::make_cell('Dato', 15, 1),
                self::make_cell($from_date->getDateStr(), 39),
                self::make_cell('Dag', 15, 1),
                self::make_cell($from_date->getDayStr(), 33),
                self::make_cell('Uke', 15, 1),
                self::make_cell($from_date->getWeekStr(), 33)),
            self::make_row(self::make_cell('Arr.ment/Arrangør', 46, 1),
                self::make_cell($shift->Event->name, 150)),
            self::make_row(self::make_cell('Arrangement sted', 46, 1),
                self::make_cell($shift->Event->location, 150)),
            self::make_row(self::make_cell('Oppmøte depot', 46, 1),
                self::make_cell($appearanceString, 30),
                self::make_cell('Oppmøte arr.', 30, 1),
                self::make_cell($from_date->getTimeStr(), 30),
                self::make_cell('Antatt ferdig', 30, 1),
                self::make_cell($to_date->getTimeStr(), 30)),
            self::make_row(self::make_cell('Kontaktperson TRKH', 46, 1),
                self::make_cell($shift->Event->Member->first_name . ' ' . $shift->Event->Member->last_name, 90, 0),
                self::make_cell('Telefon', 30, 1),
                self::make_cell($shift->Event->Member->Phonenumbers->getFirst()->number, 30)),
            self::make_row(self::make_cell('Kontaktperson arrangør', 46, 1),
                self::make_cell($shift->Event->contact_name, 90),
                self::make_cell('Telefon', 30, 1),
                self::make_cell($shift->Event->contact_phone, 30)));

        if (!is_null($shift->special_description))
        {
            $noti[] = self::make_row(self::make_cell('Spes.Opplysninger', 46, 1),
                self::make_cell($shift->special_description, 150));
        }
        if (!is_null($shift->equipment_list))
        {
            $noti[] = self::make_row(self::make_cell('Utstyr', 46, 1),
                self::make_cell($shift->equipment_list, 150));
        }

        /* TODO: who's name should we put as shift manager */
        $noti[] = self::make_row(self::make_cell('MANNSKAP', 46, 1, 'B'),
                self::make_cell('Antall mannskap', 40, 1),
                self::make_cell(count($shift->Roles), 12),
                self::make_cell('Vaktutkaller', 46, 1),
                self::make_cell($shift->Manager->first_name .
                    ' ' . $shift->Manager->last_name, 52));

        $staff = array();
        $leader = array();
        foreach ($shift->Roles as $role)
        {
            if ($role->leader)
                $leader[] = $role;
            else
                $staff[] = $role;
        }
        usort($staff, array('RedCross_Global', 'shift_role_sort_cmp'));

        foreach ($leader as $lead)
        {
            $noti[] = self::make_row(self::make_cell($lead->Role->name . ' (Vaktleder)', 46, 1),
                    self::make_cell($lead->Member->first_name . ' ' .
                        $lead->Member->last_name, 52),
                    self::make_cell('Tlf. Vaktleder', 46, 1),
                    self::make_cell($lead->Member->Phonenumbers->getFirst()->number,
                        52));
        }

        $i = 0;
        $whole = count($staff) - count($staff) % 2;
        for (; $i < $whole; $i += 2)
        {
            $noti[] = self::make_row(self::make_cell($staff[$i]->Role->name, 46, 1),
                    self::make_cell($staff[$i]->Member->first_name . ' ' .
                        $staff[$i]->Member->last_name, 52, 0),
                    self::make_cell($staff[$i + 1]->Role->name, 46, 1),
                    self::make_cell($staff[$i + 1]->Member->first_name . ' ' .
                    $staff[$i + 1]->Member->last_name, 52, 0));
        }

        for (; $i < count($staff); ++$i)
        {
            $noti[] = self::make_row(self::make_cell($staff[$i]->Role->name, 46, 1),
                    self::make_cell($staff[$i]->Member->first_name . ' ' .
                        $staff[$i]->Member->last_name, 150, 0));
        }
        
        // Reminder: You are self responsible for changing shifts if you can't attend yourself
        $noti[] = self::make_row(self::make_cell('HUSK: Det er ditt eget ansvar å bytte bort vakten hvis du ikke selv kan ta den.', 196));

        foreach ($noti as $row)
        {
            $this->MultiRow($row);
            $this->Ln();
        }
    }
}
/*$test = new RedCross_Pdf_ShiftNotification();
$test->fill();
$test->Output();*/
